package com.hivekion.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hivekion.common.annotation.AutoLog;
import com.hivekion.common.entity.PagedResultVo;
import com.hivekion.common.enums.OperationTypeEnum;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.domain.vo.user.*;
import com.hivekion.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息
 *
 * @author sutao
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @GetMapping("/list")
    public PagedResultVo<SysUserViewVo> list(SysUserSearchPageInputVo inputVo) {
        PagedResultVo<SysUserViewVo> list = userService.selectUserList(inputVo);
        return list;
    }

    @GetMapping("/getUserName")
    public String getUserName(String userIds) {
        return userService.getUserName(userIds);
    }

    @PostMapping("/create")
    @PreAuthorize("@Permission.hasPermi('cf:sys:user:add')")
    @AutoLog(value = "新增用户", operationType = OperationTypeEnum.INSERT, module = "系统管理/用户管理")
    public boolean create(@RequestBody SysUserCreateInputVo inputDto) {
        if (inputDto.getUserName() == null) {
            throw new BusinessException(500, "新增用户的账号不能为空！");
        }
        if (inputDto.getPassword() == null) {
            throw new BusinessException(500, "新增用户的密码不能为空！");
        }
        if (StringUtils.isBlank(inputDto.getUserName())) {
            throw new BusinessException(500, "新增用户'" + inputDto.getUserName() + "'失败，登录账号不能为空");
        }
        if (!userService.checkUserNameUnique(inputDto.getUserName(), "")) {
            throw new BusinessException(500, "新增用户'" + inputDto.getUserName() + "'失败，登录账号已存在");
        }
        return userService.create(inputDto);
    }

    @PutMapping("/update")
    @AutoLog(value = "变更用户信息", operationType = OperationTypeEnum.UPDATE, module = "系统管理/用户管理")
    @PreAuthorize("@Permission.hasPermi('cf:sys:user:edit')")
    public boolean update(@RequestBody SysUserUpdateInputVo inputVo) {

        SysUserModelVo info = userService.getInfo(inputVo.getId());

        if (info != null
                && !info.getUserName().equals(inputVo.getUserName())
                && !userService.checkUserNameUnique(inputVo.getUserName(), inputVo.getId())) {
            throw new BusinessException(500, "修改用户'" + inputVo.getUserName() + "'失败，登录账号已存在");
        }
        return userService.update(inputVo);
    }

    @DeleteMapping("/delete")
    @PreAuthorize("@Permission.hasPermi('cf:sys:user:remove')")
    @AutoLog(value = "删除用户信息", operationType = OperationTypeEnum.DELETE, module = "系统管理/用户管理")
    public boolean delete(String userId) {
        return userService.delete(userId);
    }

    @PostMapping("/resetPwd")
    @PreAuthorize("@Permission.hasPermi('cf:sys:user:resetPwd')")
    @AutoLog(value = "重置用户密码", operationType = OperationTypeEnum.UPDATE, module = "系统管理/用户管理")
    public boolean resetPwd(SysUserResetPwdVo pwdVo) {
        return userService.resetPwd(pwdVo);
    }

    @GetMapping("/getInfo")
    @AutoLog(value = "用户管理", operationType = OperationTypeEnum.SELECT, module = "系统管理/用户管理")
    public SysUserModelVo getInfo(String userId) {
        return userService.getInfo(userId);
    }

}
